package com.xpgk.mvc.bentryRegister;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import com.jfinal.log.Log;
import com.xpgk.annotation.Table;
import com.xpgk.mvc.base.BaseModel;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.dept.Department;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.scallVisit.ScallVisit;
import com.xpgk.tools.security.ToolPbkdf2;

/**
 * 顾客信息登记model
 */
@SuppressWarnings("unused")
@Table(tableName = Customer.table_name)
public class Customer extends BaseModel<Customer> {

	private static final long serialVersionUID = -6304007923659688302L;

	private static final Log log = Log.getLog(Customer.class);
	public static final Customer dao = new Customer().dao();
	
	
	/**
	 * 表名称
	 */
	public static final String table_name = "sb_customer";

	/**
	 * 字段描述：状态码（成功） 字段类型：character varying 长度：11
	 */
	public static final String column_success = "200";

	/**
	 * 字段描述：ids 字段类型：character varying 长度：32
	 */
	public static final String column_ids = "ids";
	/**
	 * 字段描述：机构ids 字段类型：character varying 长度：32
	 */
	public static final String column_organIds = "organIds";

	/**
	 * 字段描述：登录用户名字段类型：character varying 长度：50
	 */
	public static final String column_userName = "userName";

	/**
	 * 字段描述：密码 字段类型：character varying 长度：255
	 */
	public static final String column_password = "password";
	
	/**
	 * 字段描述：密码盐值
	 */
	public static final String column_salt = "salt";
	
	/**
	 * 字段描述：会员编号 字段类型：character varying 长度：255
	 */
	public static final String column_hybh = "hybh";
	
	/**
	 * 字段描述：姓名 字段类型：character varying 长度：255
	 */
	public static final String column_name = "name";

	/**
	 * 字段描述：性别 字段类型：character varying 长度：5
	 */
	public static final String column_sex = "sex";

	/**
	 * 字段描述：年龄 字段类型：bigint 长度：11
	 */
	public static final String column_age = "age";

	/**
	 * 字段描述：身份证 字段类型：character varying 长度：18
	 */
	public static final String column_sfzmhm = "sfzmhm";

	/**
	 * 字段描述：手机号 字段类型 ：character varying 长度：11
	 */
	public static final String column_phone = "phone";
	
	/**
	 * 字段描述：email 字段类型 ：character varying 长度：11
	 */
	public static final String column_email = "email";
	
	/**
	 * 字段描述：部门编号 字段类型 ：character varying 长度：20
	 */
	public static final String column_departmentbh = "departmentbh";
	
	/**
	 * 字段描述：类型 字段类型 ：character varying 长度：1
	 */
	public static final String column_type = "type";
	
	/**
	 * 字段描述：注册时间 字段类型 ：datatime
	 */
	public static final String column_registtime = "registtime";
	
	/**
	 * 字段描述：最后登录时间 字段类型 ：datatime
	 */
	public static final String column_lastlogintime = "lastlogintime";
	
	/**
	 * 字段描述：登陆ip 字段类型 ：character varying 长度：100
	 */
	public static final String column_limitIP = "limitIP";
	/**
	 * 字段描述：状态  字段类型：int 长度：32
	 */
	public static final String column_dataStatus = "dataStatus";
	/**
	 * 字段描述：状态 字段类型 ：character varying 长度：1
	 */
	public static final String column_state = "state";

	/**
	 * 字段描述：正面照 字段类型 ：character varying 长度：100
	 */
	public static final String column_zmPicture = "zmPicture";
	
	/**
	 * 字段描述：侧面照 字段类型 ：character varying 长度：100
	 */
	public static final String column_cmPicture = "cmPicture";
	/**
	 * 字段描述：该条数据添加人员is  字段类型：character varying 长度：32
	 */
	public static final String column_createId = "createId";
	/**
	 * 字段描述：该条数据添加时间  字段类型：datetime 长度：0
	 */
	public static final String column_createDate = "createDate";
	/**
	 * 字段描述：该条数据更新人员id  字段类型：character varying 长度：100
	 */
	public static final String column_updateId = "updateId";
	/**
	 * 字段描述：该条数据更新时间  字段类型：datetime 长度：0
	 */
	public static final String column_updateDate = "updateDate";
	
	/**
	 * 字段描述：微信id  字段类型：datetime 长度：0
	 */
	public static final String column_openid = "openid";
	/**
	 * 字段描述：该条数据更新时间  字段类型：datetime 长度：0
	 */
	//public static final String column_dataStatus = "dataStatus";

	/**
	 * sqlId :sb.customer.column 描述：查询顾客信息，自定义字段
	 */
	public static final String sqlId_column = "sb.customer.column";
	
	/**
	 * sqlId :sb.customer.selCus 描述：根据身份证和姓名查出顾客
	 */
	public static final String sqlId_selCus = "sb.customer.selCus1";
	
	/**
	 * sqlId :sb.customer.selCusByHybh 描述：根据会员编号模糊查询
	 */
	public static final String sqlId_selCusByHybh = "sb.customer.selCusByHybh";
	
	/**
	 * sqlId :sb.customer.selCusByHybhAndOrg 描述：根据会员编号和机构ids查询
	 */
	public static final String sqlId_selCusByHybhAndOrg = "sb.customer.selCusByHybhAndOrg";
	
	/**
	 * sqlId :sb.customer.selOrgByCusPhone 描述：根据会员手机号查出所在门店
	 */
	public static final String sqlId_selOrgByCusPhone1 = "sb.customer.selOrgByCusPhone1";

	/**
	 * 通过username 查找
	 */
	public static final String sqlId_selCusByUserName = "sb.customer.selCusByUserName";

	/**
	 * 关联查询，获取顾客详细信息
	 * 
	 * @return
	 */
	public CustomerDetail getCustomerDetail() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", CustomerDetail.column_customerIds);
		String sql = getSqlByBeetl(CustomerDetail.sqlId_column, param);
		log.info("转店"+this.getStr(column_ids));
		return CustomerDetail.dao.findFirst(sql, this.getStr(column_ids));
	}

	/**
	 * 关联查询，获取机构信息
	 * 
	 * @return
	 */
	public Organization getOrganization() {

		return Organization.dao.findById(this.getStr(column_organIds));
	}

	/**
	 * 关联查询，获取顾客协议
	 * 
	 * @return
	 */
	public CustomerAgreement getCustomerAgreement() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", CustomerAgreement.column_customerID);
		String sql = getSqlByBeetl(CustomerAgreement.sqlId_column, param);
		return CustomerAgreement.dao.findFirst(sql, this.getStr(column_ids));
	}

	/**
	 * 关联查询，获取到店次数
	 * 
	 * @return
	 */
	public CustomerService getCustomerServiceCount(String customerIds, String organIds) {
		String sql = "SELECT  COUNT(ids) count  from  sb_customer_service  where  customerID=?  and  organids=? and dataStatus in ('1','2')";
		return CustomerService.dao.findFirst(sql, customerIds, organIds);
	}

	/**
	 * 关联查询，获取服务记录
	 * 
	 * @return
	 */
	public List<CustomerService> getCustomerService(String customerIds, String organIds) {
		String sql = "SELECT  *   from  sb_customer_service  where  customerID=?  and  organids=? and dataStatus in ('1','2') ORDER by  date";
		return CustomerService.dao.find(sql, customerIds, organIds);
	}

	/**
	 * 密码验证
	 * 
	 * @param password
	 * @return
	 */
	public boolean checkPwd(String password) {
		String saltStr = this.getStr("salt");
		String passStr = this.getStr("password");
		byte[] salt = Base64.decodeBase64(saltStr);
		byte[] encryptedPassword = Base64.decodeBase64(passStr);
		boolean bool = false;
		try {
			bool = ToolPbkdf2.authenticate(password, encryptedPassword, salt);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * 验证账号状态
	 * @return
	 */
	public boolean isValid(){
		return this.getStr("state").equals("1");
	}

}
